<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns:yui="http://yuilibrary.com/rdf/1.0/yui.rdf#">
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
	<title>API: json   JSON.js  (YUI Library)</title>

	<link rel="stylesheet" type="text/css" href="assets/reset-fonts-grids-min.css" />
	<link rel="stylesheet" type="text/css" href="assets/api.css" />

    <script type="text/javascript" src="assets/api-js"></script>
    <script type="text/javascript" src="assets/ac-js"></script>
</head>

<body id="yahoo-com">

<div id="doc3" class="yui-t2">
	<div id="hd">
        <h1><a href="http://developer.yahoo.com/yui/" title="Yahoo! UI Library">Yahoo! UI Library</a></h1>
        <h3>json&nbsp; <span class="subtitle">2.8.1</span></h3>
        <a href="./index.html" title="Yahoo! UI Library">Yahoo! UI Library</a> 
            &gt; <a href="./module_json.html" title="json">json</a>
                
                 &gt; JSON.js (source view) 
        <form onsubmit="return false">
            <div id="propertysearch">
                Search: <input autocomplete="off" id="searchinput" />
                <div id="searchresults">
                    &nbsp;
                </div>
            </div>
        </form>
	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">
            <form action="#" name="yui-classopts-form" method="get" id="yui-classopts-form">
                <fieldset>
                    <legend>Filters</legend>
                <span class="classopts"><input type="checkbox" name="show_private" id="show_private" /> <label for="show_private">Show Private</label></span>
                <span class="classopts"><input type="checkbox" name="show_protected" id="show_protected" /> <label for="show_protected">Show Protected</label></span>
                <span class="classopts"><input type="checkbox" name="show_deprecated" id="show_deprecated" /> <label for="show_deprecated">Show Deprecated</label></span>
                </fieldset>
            </form>

                    <div id="srcout">
                        <style>
                            #doc3 .classopts { display:none; }
                        </style>
                        <div class="highlight"><pre><span class="cm">/**</span>
<span class="cm"> * Provides methods to parse JSON strings and convert objects to JSON strings.</span>
<span class="cm"> *</span>
<span class="cm"> * @module json</span>
<span class="cm"> * @class JSON</span>
<span class="cm"> * @namespace YAHOO.lang</span>
<span class="cm"> * @static</span>
<span class="cm"> */</span>
<span class="p">(</span><span class="kd">function</span> <span class="p">()</span> <span class="p">{</span>

<span class="kd">var</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">,</span>
    <span class="nx">isFunction</span> <span class="o">=</span> <span class="nx">l</span><span class="p">.</span><span class="nx">isFunction</span><span class="p">,</span>
    <span class="nx">isObject</span>   <span class="o">=</span> <span class="nx">l</span><span class="p">.</span><span class="nx">isObject</span><span class="p">,</span>
    <span class="nx">isArray</span>    <span class="o">=</span> <span class="nx">l</span><span class="p">.</span><span class="nx">isArray</span><span class="p">,</span>
    <span class="nx">_toStr</span>     <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">prototype</span><span class="p">.</span><span class="nx">toString</span><span class="p">,</span>
                 <span class="c1">// &#39;this&#39; is the global object.  window in browser env.  Keep</span>
                 <span class="c1">// the code env agnostic.  Caja requies window, unfortunately.</span>
    <span class="nx">Native</span>     <span class="o">=</span> <span class="p">(</span><span class="nx">YAHOO</span><span class="p">.</span><span class="nx">env</span><span class="p">.</span><span class="nx">ua</span><span class="p">.</span><span class="nx">caja</span> <span class="o">?</span> <span class="nb">window</span> <span class="o">:</span> <span class="k">this</span><span class="p">).</span><span class="nx">JSON</span><span class="p">,</span>

<span class="cm">/* Variables used by parse */</span>

    <span class="cm">/**</span>
<span class="cm">     * Replace certain Unicode characters that JavaScript may handle incorrectly</span>
<span class="cm">     * during eval--either by deleting them or treating them as line</span>
<span class="cm">     * endings--with escape sequences.</span>
<span class="cm">     * IMPORTANT NOTE: This regex will be used to modify the input if a match is</span>
<span class="cm">     * found.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _UNICODE_EXCEPTIONS</span>
<span class="cm">     * @type {RegExp}</span>
<span class="cm">     * @private</span>
<span class="cm">     */</span>
    <span class="nx">_UNICODE_EXCEPTIONS</span> <span class="o">=</span> <span class="sr">/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * First step in the safety evaluation.  Regex used to replace all escape</span>
<span class="cm">     * sequences (i.e. &quot;\\&quot;, etc) with &#39;@&#39; characters (a non-JSON character).</span>
<span class="cm">     *</span>
<span class="cm">     * @property _ESCAPES</span>
<span class="cm">     * @type {RegExp}</span>
<span class="cm">     * @static</span>
<span class="cm">     * @private</span>
<span class="cm">     */</span>
    <span class="nx">_ESCAPES</span> <span class="o">=</span> <span class="sr">/\\(?:[&quot;\\\/bfnrt]|u[0-9a-fA-F]{4})/g</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Second step in the safety evaluation.  Regex used to replace all simple</span>
<span class="cm">     * values with &#39;]&#39; characters.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _VALUES</span>
<span class="cm">     * @type {RegExp}</span>
<span class="cm">     * @static</span>
<span class="cm">     * @private</span>
<span class="cm">     */</span>
    <span class="nx">_VALUES</span>  <span class="o">=</span> <span class="sr">/&quot;[^&quot;\\\n\r]*&quot;|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Third step in the safety evaluation.  Regex used to remove all open</span>
<span class="cm">     * square brackets following a colon, comma, or at the beginning of the</span>
<span class="cm">     * string.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _BRACKETS</span>
<span class="cm">     * @type {RegExp}</span>
<span class="cm">     * @static</span>
<span class="cm">     * @private</span>
<span class="cm">     */</span>
    <span class="nx">_BRACKETS</span> <span class="o">=</span> <span class="sr">/(?:^|:|,)(?:\s*\[)+/g</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Final step in the safety evaluation.  Regex used to test the string left</span>
<span class="cm">     * after all previous replacements for invalid characters.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _UNSAFE</span>
<span class="cm">     * @type {RegExp}</span>
<span class="cm">     * @static</span>
<span class="cm">     * @private</span>
<span class="cm">     */</span>
    <span class="nx">_UNSAFE</span>  <span class="o">=</span> <span class="sr">/^[\],:{}\s]*$/</span><span class="p">,</span>


<span class="cm">/* Variables used by stringify */</span>

    <span class="cm">/**</span>
<span class="cm">     * Regex used to replace special characters in strings for JSON</span>
<span class="cm">     * stringification.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _SPECIAL_CHARS</span>
<span class="cm">     * @type {RegExp}</span>
<span class="cm">     * @static</span>
<span class="cm">     * @private</span>
<span class="cm">     */</span>
    <span class="nx">_SPECIAL_CHARS</span> <span class="o">=</span> <span class="sr">/[\\\&quot;\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Character substitution map for common escapes and special characters.</span>
<span class="cm">     *</span>
<span class="cm">     * @property _CHARS</span>
<span class="cm">     * @type {Object}</span>
<span class="cm">     * @static</span>
<span class="cm">     * @private</span>
<span class="cm">     */</span>
    <span class="nx">_CHARS</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;\b&#39;</span><span class="o">:</span> <span class="s1">&#39;\\b&#39;</span><span class="p">,</span>
        <span class="s1">&#39;\t&#39;</span><span class="o">:</span> <span class="s1">&#39;\\t&#39;</span><span class="p">,</span>
        <span class="s1">&#39;\n&#39;</span><span class="o">:</span> <span class="s1">&#39;\\n&#39;</span><span class="p">,</span>
        <span class="s1">&#39;\f&#39;</span><span class="o">:</span> <span class="s1">&#39;\\f&#39;</span><span class="p">,</span>
        <span class="s1">&#39;\r&#39;</span><span class="o">:</span> <span class="s1">&#39;\\r&#39;</span><span class="p">,</span>
        <span class="s1">&#39;&quot;&#39;</span> <span class="o">:</span> <span class="s1">&#39;\\&quot;&#39;</span><span class="p">,</span>
        <span class="s1">&#39;\\&#39;</span><span class="o">:</span> <span class="s1">&#39;\\\\&#39;</span>
    <span class="p">},</span>
    
    <span class="nx">UNDEFINED</span> <span class="o">=</span> <span class="s1">&#39;undefined&#39;</span><span class="p">,</span>
    <span class="nx">OBJECT</span>    <span class="o">=</span> <span class="s1">&#39;object&#39;</span><span class="p">,</span>
    <span class="nx">NULL</span>      <span class="o">=</span> <span class="s1">&#39;null&#39;</span><span class="p">,</span>
    <span class="nx">STRING</span>    <span class="o">=</span> <span class="s1">&#39;string&#39;</span><span class="p">,</span>
    <span class="nx">NUMBER</span>    <span class="o">=</span> <span class="s1">&#39;number&#39;</span><span class="p">,</span>
    <span class="nx">BOOLEAN</span>   <span class="o">=</span> <span class="s1">&#39;boolean&#39;</span><span class="p">,</span>
    <span class="nx">DATE</span>      <span class="o">=</span> <span class="s1">&#39;date&#39;</span><span class="p">,</span>
    <span class="nx">_allowable</span> <span class="o">=</span> <span class="p">{</span>
        <span class="s1">&#39;undefined&#39;</span>        <span class="o">:</span> <span class="nx">UNDEFINED</span><span class="p">,</span>
        <span class="s1">&#39;string&#39;</span>           <span class="o">:</span> <span class="nx">STRING</span><span class="p">,</span>
        <span class="s1">&#39;[object String]&#39;</span>  <span class="o">:</span> <span class="nx">STRING</span><span class="p">,</span>
        <span class="s1">&#39;number&#39;</span>           <span class="o">:</span> <span class="nx">NUMBER</span><span class="p">,</span>
        <span class="s1">&#39;[object Number]&#39;</span>  <span class="o">:</span> <span class="nx">NUMBER</span><span class="p">,</span>
        <span class="s1">&#39;boolean&#39;</span>          <span class="o">:</span> <span class="nx">BOOLEAN</span><span class="p">,</span>
        <span class="s1">&#39;[object Boolean]&#39;</span> <span class="o">:</span> <span class="nx">BOOLEAN</span><span class="p">,</span>
        <span class="s1">&#39;[object Date]&#39;</span>    <span class="o">:</span> <span class="nx">DATE</span><span class="p">,</span>
        <span class="s1">&#39;[object RegExp]&#39;</span>  <span class="o">:</span> <span class="nx">OBJECT</span>
    <span class="p">},</span>
    <span class="nx">EMPTY</span>     <span class="o">=</span> <span class="s1">&#39;&#39;</span><span class="p">,</span>
    <span class="nx">OPEN_O</span>    <span class="o">=</span> <span class="s1">&#39;{&#39;</span><span class="p">,</span>
    <span class="nx">CLOSE_O</span>   <span class="o">=</span> <span class="s1">&#39;}&#39;</span><span class="p">,</span>
    <span class="nx">OPEN_A</span>    <span class="o">=</span> <span class="s1">&#39;[&#39;</span><span class="p">,</span>
    <span class="nx">CLOSE_A</span>   <span class="o">=</span> <span class="s1">&#39;]&#39;</span><span class="p">,</span>
    <span class="nx">COMMA</span>     <span class="o">=</span> <span class="s1">&#39;,&#39;</span><span class="p">,</span>
    <span class="nx">COMMA_CR</span>  <span class="o">=</span> <span class="s2">&quot;,\n&quot;</span><span class="p">,</span>
    <span class="nx">CR</span>        <span class="o">=</span> <span class="s2">&quot;\n&quot;</span><span class="p">,</span>
    <span class="nx">COLON</span>     <span class="o">=</span> <span class="s1">&#39;:&#39;</span><span class="p">,</span>
    <span class="nx">COLON_SP</span>  <span class="o">=</span> <span class="s1">&#39;: &#39;</span><span class="p">,</span>
    <span class="nx">QUOTE</span>     <span class="o">=</span> <span class="s1">&#39;&quot;&#39;</span><span class="p">;</span>

<span class="c1">// Only accept JSON objects that report a [[Class]] of JSON</span>
<span class="nx">Native</span> <span class="o">=</span> <span class="nx">_toStr</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">Native</span><span class="p">)</span> <span class="o">===</span> <span class="s1">&#39;[object JSON]&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">Native</span><span class="p">;</span>

<span class="c1">// Escapes a special character to a safe Unicode representation</span>
<span class="kd">function</span> <span class="nx">_char</span><span class="p">(</span><span class="nx">c</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="nx">_CHARS</span><span class="p">[</span><span class="nx">c</span><span class="p">])</span> <span class="p">{</span>
        <span class="nx">_CHARS</span><span class="p">[</span><span class="nx">c</span><span class="p">]</span> <span class="o">=</span>  <span class="s1">&#39;\\u&#39;</span><span class="o">+</span><span class="p">(</span><span class="s1">&#39;0000&#39;</span><span class="o">+</span><span class="p">(</span><span class="o">+</span><span class="p">(</span><span class="nx">c</span><span class="p">.</span><span class="nx">charCodeAt</span><span class="p">(</span><span class="mi">0</span><span class="p">))).</span><span class="nx">toString</span><span class="p">(</span><span class="mi">16</span><span class="p">)).</span><span class="nx">slice</span><span class="p">(</span><span class="o">-</span><span class="mi">4</span><span class="p">);</span>
    <span class="p">}</span>
    <span class="k">return</span> <span class="nx">_CHARS</span><span class="p">[</span><span class="nx">c</span><span class="p">];</span>
<span class="p">}</span>


<span class="cm">/* functions used by parse */</span>

<span class="cm">/**</span>
<span class="cm"> * Traverses nested objects, applying a filter or reviver function to</span>
<span class="cm"> * each value.  The value returned from the function will replace the</span>
<span class="cm"> * original value in the key:value pair.  If the value returned is</span>
<span class="cm"> * undefined, the key will be omitted from the returned object.</span>
<span class="cm"> *</span>
<span class="cm"> * @method _revive</span>
<span class="cm"> * @param data {MIXED} Any JavaScript data</span>
<span class="cm"> * @param reviver {Function} filter or mutation function</span>
<span class="cm"> * @return {MIXED} The results of the filtered/mutated data structure</span>
<span class="cm"> * @private</span>
<span class="cm"> */</span>
<span class="kd">function</span> <span class="nx">_revive</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">reviver</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">walk</span> <span class="o">=</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">k</span><span class="p">,</span><span class="nx">v</span><span class="p">,</span><span class="nx">value</span> <span class="o">=</span> <span class="nx">o</span><span class="p">[</span><span class="nx">key</span><span class="p">];</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">value</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">value</span> <span class="o">===</span> <span class="s1">&#39;object&#39;</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">for</span> <span class="p">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="nx">l</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span><span class="nx">k</span><span class="p">))</span> <span class="p">{</span>
                    <span class="nx">v</span> <span class="o">=</span> <span class="nx">walk</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">k</span><span class="p">);</span>
                    <span class="k">if</span> <span class="p">(</span><span class="nx">v</span> <span class="o">===</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
                        <span class="k">delete</span> <span class="nx">value</span><span class="p">[</span><span class="nx">k</span><span class="p">];</span>
                    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
                        <span class="nx">value</span><span class="p">[</span><span class="nx">k</span><span class="p">]</span> <span class="o">=</span> <span class="nx">v</span><span class="p">;</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="nx">reviver</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="nx">key</span><span class="p">,</span><span class="nx">value</span><span class="p">);</span>
    <span class="p">};</span>

    <span class="k">return</span> <span class="k">typeof</span> <span class="nx">reviver</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">?</span> <span class="nx">walk</span><span class="p">({</span><span class="s1">&#39;&#39;</span><span class="o">:</span><span class="nx">data</span><span class="p">},</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="o">:</span> <span class="nx">data</span><span class="p">;</span>
<span class="p">}</span>

<span class="cm">/**</span>
<span class="cm"> * Replace certain Unicode characters that may be handled incorrectly by</span>
<span class="cm"> * some browser implementations.</span>
<span class="cm"> *</span>
<span class="cm"> * @method _prepare</span>
<span class="cm"> * @param s {String} parse input</span>
<span class="cm"> * @return {String} sanitized JSON string ready to be validated/parsed</span>
<span class="cm"> * @private</span>
<span class="cm"> */</span>
<span class="kd">function</span> <span class="nx">_prepare</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">s</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">_UNICODE_EXCEPTIONS</span><span class="p">,</span> <span class="nx">_char</span><span class="p">);</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">_isSafe</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">l</span><span class="p">.</span><span class="nx">isString</span><span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="o">&amp;&amp;</span>
            <span class="nx">_UNSAFE</span><span class="p">.</span><span class="nx">test</span><span class="p">(</span><span class="nx">str</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">_ESCAPES</span><span class="p">,</span><span class="s1">&#39;@&#39;</span><span class="p">).</span>
                             <span class="nx">replace</span><span class="p">(</span><span class="nx">_VALUES</span><span class="p">,</span><span class="s1">&#39;]&#39;</span><span class="p">).</span>
                             <span class="nx">replace</span><span class="p">(</span><span class="nx">_BRACKETS</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">));</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">_parse</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="nx">reviver</span><span class="p">)</span> <span class="p">{</span>
    <span class="c1">// sanitize</span>
    <span class="nx">s</span> <span class="o">=</span> <span class="nx">_prepare</span><span class="p">(</span><span class="nx">s</span><span class="p">);</span>

    <span class="c1">// Ensure valid JSON</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">_isSafe</span><span class="p">(</span><span class="nx">s</span><span class="p">))</span> <span class="p">{</span>
        <span class="c1">// Eval the text into a JavaScript data structure, apply the</span>
        <span class="c1">// reviver function if provided, and return</span>
        <span class="k">return</span> <span class="nx">_revive</span><span class="p">(</span> <span class="nb">eval</span><span class="p">(</span><span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">s</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="p">),</span> <span class="nx">reviver</span> <span class="p">);</span>
    <span class="p">}</span>

    <span class="c1">// The text is not valid JSON</span>
    <span class="k">throw</span> <span class="k">new</span> <span class="nx">SyntaxError</span><span class="p">(</span><span class="s1">&#39;JSON.parse&#39;</span><span class="p">);</span>
<span class="p">}</span>



<span class="cm">/* functions used by stringify */</span>

<span class="c1">// Utility function used to determine how to serialize a variable.</span>
<span class="kd">function</span> <span class="nx">_type</span><span class="p">(</span><span class="nx">o</span><span class="p">)</span> <span class="p">{</span>
    <span class="kd">var</span> <span class="nx">t</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">o</span><span class="p">;</span>
    <span class="k">return</span>  <span class="nx">_allowable</span><span class="p">[</span><span class="nx">t</span><span class="p">]</span> <span class="o">||</span>              <span class="c1">// number, string, boolean, undefined</span>
            <span class="nx">_allowable</span><span class="p">[</span><span class="nx">_toStr</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">o</span><span class="p">)]</span> <span class="o">||</span> <span class="c1">// Number, String, Boolean, Date</span>
            <span class="p">(</span><span class="nx">t</span> <span class="o">===</span> <span class="nx">OBJECT</span> <span class="o">?</span>
                <span class="p">(</span><span class="nx">o</span> <span class="o">?</span> <span class="nx">OBJECT</span> <span class="o">:</span> <span class="nx">NULL</span><span class="p">)</span> <span class="o">:</span>     <span class="c1">// object, array, null, misc natives</span>
                <span class="nx">UNDEFINED</span><span class="p">);</span>               <span class="c1">// function, unknown</span>
<span class="p">}</span>

<span class="c1">// Enclose escaped strings in quotes</span>
<span class="kd">function</span> <span class="nx">_string</span><span class="p">(</span><span class="nx">s</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">QUOTE</span> <span class="o">+</span> <span class="nx">s</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="nx">_SPECIAL_CHARS</span><span class="p">,</span> <span class="nx">_char</span><span class="p">)</span> <span class="o">+</span> <span class="nx">QUOTE</span><span class="p">;</span>
<span class="p">}</span>

<span class="c1">// Adds the provided space to the beginning of every line in the input string</span>
<span class="kd">function</span> <span class="nx">_indent</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="nx">space</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="nx">s</span><span class="p">.</span><span class="nx">replace</span><span class="p">(</span><span class="sr">/^/gm</span><span class="p">,</span> <span class="nx">space</span><span class="p">);</span>
<span class="p">}</span>

<span class="c1">// JavaScript implementation of stringify (see API declaration of stringify)</span>
<span class="kd">function</span> <span class="nx">_stringify</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="nx">w</span><span class="p">,</span><span class="nx">space</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">o</span> <span class="o">===</span> <span class="kc">undefined</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="kc">undefined</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="kd">var</span> <span class="nx">replacer</span> <span class="o">=</span> <span class="nx">isFunction</span><span class="p">(</span><span class="nx">w</span><span class="p">)</span> <span class="o">?</span> <span class="nx">w</span> <span class="o">:</span> <span class="kc">null</span><span class="p">,</span>
        <span class="nx">format</span>   <span class="o">=</span> <span class="nx">_toStr</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">space</span><span class="p">).</span><span class="nx">match</span><span class="p">(</span><span class="sr">/String|Number/</span><span class="p">)</span> <span class="o">||</span> <span class="p">[],</span>
        <span class="nx">_date</span>    <span class="o">=</span> <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">dateToString</span><span class="p">,</span>
        <span class="nx">stack</span>    <span class="o">=</span> <span class="p">[],</span>
        <span class="nx">tmp</span><span class="p">,</span><span class="nx">i</span><span class="p">,</span><span class="nx">len</span><span class="p">;</span>

    <span class="k">if</span> <span class="p">(</span><span class="nx">replacer</span> <span class="o">||</span> <span class="o">!</span><span class="nx">isArray</span><span class="p">(</span><span class="nx">w</span><span class="p">))</span> <span class="p">{</span>
        <span class="nx">w</span> <span class="o">=</span> <span class="kc">undefined</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="c1">// Ensure whitelist keys are unique (bug 2110391)</span>
    <span class="k">if</span> <span class="p">(</span><span class="nx">w</span><span class="p">)</span> <span class="p">{</span>
        <span class="nx">tmp</span> <span class="o">=</span> <span class="p">{};</span>
        <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">w</span><span class="p">.</span><span class="nx">length</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="p">;</span> <span class="o">++</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">tmp</span><span class="p">[</span><span class="nx">w</span><span class="p">[</span><span class="nx">i</span><span class="p">]]</span> <span class="o">=</span> <span class="kc">true</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="nx">w</span> <span class="o">=</span> <span class="nx">tmp</span><span class="p">;</span>
    <span class="p">}</span>

    <span class="c1">// Per the spec, strings are truncated to 10 characters and numbers</span>
    <span class="c1">// are converted to that number of spaces (max 10)</span>
    <span class="nx">space</span> <span class="o">=</span> <span class="nx">format</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">===</span> <span class="s1">&#39;Number&#39;</span> <span class="o">?</span>
                <span class="k">new</span> <span class="nb">Array</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">min</span><span class="p">(</span><span class="nb">Math</span><span class="p">.</span><span class="nx">max</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="nx">space</span><span class="p">),</span><span class="mi">10</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">).</span><span class="nx">join</span><span class="p">(</span><span class="s2">&quot; &quot;</span><span class="p">)</span> <span class="o">:</span>
                <span class="p">(</span><span class="nx">space</span> <span class="o">||</span> <span class="nx">EMPTY</span><span class="p">).</span><span class="nx">slice</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">10</span><span class="p">);</span>

    <span class="kd">function</span> <span class="nx">_serialize</span><span class="p">(</span><span class="nx">h</span><span class="p">,</span><span class="nx">key</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">value</span> <span class="o">=</span> <span class="nx">h</span><span class="p">[</span><span class="nx">key</span><span class="p">],</span>
            <span class="nx">t</span>     <span class="o">=</span> <span class="nx">_type</span><span class="p">(</span><span class="nx">value</span><span class="p">),</span>
            <span class="nx">a</span>     <span class="o">=</span> <span class="p">[],</span>
            <span class="nx">colon</span> <span class="o">=</span> <span class="nx">space</span> <span class="o">?</span> <span class="nx">COLON_SP</span> <span class="o">:</span> <span class="nx">COLON</span><span class="p">,</span>
            <span class="nx">arr</span><span class="p">,</span> <span class="nx">i</span><span class="p">,</span> <span class="nx">keys</span><span class="p">,</span> <span class="nx">k</span><span class="p">,</span> <span class="nx">v</span><span class="p">;</span>

        <span class="c1">// Per the ECMA 5 spec, toJSON is applied before the replacer is</span>
        <span class="c1">// called.  Also per the spec, Date.prototype.toJSON has been added, so</span>
        <span class="c1">// Date instances should be serialized prior to exposure to the</span>
        <span class="c1">// replacer.  I disagree with this decision, but the spec is the spec.</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">isObject</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">&amp;&amp;</span> <span class="nx">isFunction</span><span class="p">(</span><span class="nx">value</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">))</span> <span class="p">{</span>
            <span class="nx">value</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">toJSON</span><span class="p">(</span><span class="nx">key</span><span class="p">);</span>
        <span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">t</span> <span class="o">===</span> <span class="nx">DATE</span><span class="p">)</span> <span class="p">{</span>
            <span class="nx">value</span> <span class="o">=</span> <span class="nx">_date</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="nx">isFunction</span><span class="p">(</span><span class="nx">replacer</span><span class="p">))</span> <span class="p">{</span>
            <span class="nx">value</span> <span class="o">=</span> <span class="nx">replacer</span><span class="p">.</span><span class="nx">call</span><span class="p">(</span><span class="nx">h</span><span class="p">,</span><span class="nx">key</span><span class="p">,</span><span class="nx">value</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="k">if</span> <span class="p">(</span><span class="nx">value</span> <span class="o">!==</span> <span class="nx">h</span><span class="p">[</span><span class="nx">key</span><span class="p">])</span> <span class="p">{</span>
            <span class="nx">t</span> <span class="o">=</span> <span class="nx">_type</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="k">switch</span> <span class="p">(</span><span class="nx">t</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">case</span> <span class="nx">DATE</span>    <span class="o">:</span> <span class="c1">// intentional fallthrough.  Pre-replacer Dates are</span>
                           <span class="c1">// serialized in the toJSON stage.  Dates here would</span>
                           <span class="c1">// have been produced by the replacer.</span>
            <span class="k">case</span> <span class="nx">OBJECT</span>  <span class="o">:</span> <span class="k">break</span><span class="p">;</span>
            <span class="k">case</span> <span class="nx">STRING</span>  <span class="o">:</span> <span class="k">return</span> <span class="nx">_string</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>
            <span class="k">case</span> <span class="nx">NUMBER</span>  <span class="o">:</span> <span class="k">return</span> <span class="nb">isFinite</span><span class="p">(</span><span class="nx">value</span><span class="p">)</span> <span class="o">?</span> <span class="nx">value</span><span class="o">+</span><span class="nx">EMPTY</span> <span class="o">:</span> <span class="nx">NULL</span><span class="p">;</span>
            <span class="k">case</span> <span class="nx">BOOLEAN</span> <span class="o">:</span> <span class="k">return</span> <span class="nx">value</span><span class="o">+</span><span class="nx">EMPTY</span><span class="p">;</span>
            <span class="k">case</span> <span class="nx">NULL</span>    <span class="o">:</span> <span class="k">return</span> <span class="nx">NULL</span><span class="p">;</span>
            <span class="k">default</span>      <span class="o">:</span> <span class="k">return</span> <span class="kc">undefined</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="c1">// Check for cyclical references in nested objects</span>
        <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="nx">stack</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">;</span> <span class="o">--</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">if</span> <span class="p">(</span><span class="nx">stack</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">===</span> <span class="nx">value</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">&quot;JSON.stringify. Cyclical reference&quot;</span><span class="p">);</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="nx">arr</span> <span class="o">=</span> <span class="nx">isArray</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>

        <span class="c1">// Add the object to the processing stack</span>
        <span class="nx">stack</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="nx">value</span><span class="p">);</span>

        <span class="k">if</span> <span class="p">(</span><span class="nx">arr</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// Array</span>
            <span class="k">for</span> <span class="p">(</span><span class="nx">i</span> <span class="o">=</span> <span class="nx">value</span><span class="p">.</span><span class="nx">length</span> <span class="o">-</span> <span class="mi">1</span><span class="p">;</span> <span class="nx">i</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">;</span> <span class="o">--</span><span class="nx">i</span><span class="p">)</span> <span class="p">{</span>
                <span class="nx">a</span><span class="p">[</span><span class="nx">i</span><span class="p">]</span> <span class="o">=</span> <span class="nx">_serialize</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">i</span><span class="p">)</span> <span class="o">||</span> <span class="nx">NULL</span><span class="p">;</span>
            <span class="p">}</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>   <span class="c1">// Object</span>
            <span class="c1">// If whitelist provided, take only those keys</span>
            <span class="nx">keys</span> <span class="o">=</span> <span class="nx">w</span> <span class="o">||</span> <span class="nx">value</span><span class="p">;</span>
            <span class="nx">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

            <span class="k">for</span> <span class="p">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">keys</span><span class="p">)</span> <span class="p">{</span>
                <span class="k">if</span> <span class="p">(</span><span class="nx">keys</span><span class="p">.</span><span class="nx">hasOwnProperty</span><span class="p">(</span><span class="nx">k</span><span class="p">))</span> <span class="p">{</span>
                    <span class="nx">v</span> <span class="o">=</span> <span class="nx">_serialize</span><span class="p">(</span><span class="nx">value</span><span class="p">,</span> <span class="nx">k</span><span class="p">);</span>
                    <span class="k">if</span> <span class="p">(</span><span class="nx">v</span><span class="p">)</span> <span class="p">{</span>
                        <span class="nx">a</span><span class="p">[</span><span class="nx">i</span><span class="o">++</span><span class="p">]</span> <span class="o">=</span> <span class="nx">_string</span><span class="p">(</span><span class="nx">k</span><span class="p">)</span> <span class="o">+</span> <span class="nx">colon</span> <span class="o">+</span> <span class="nx">v</span><span class="p">;</span>
                    <span class="p">}</span>
                <span class="p">}</span>
            <span class="p">}</span>
        <span class="p">}</span>

        <span class="c1">// remove the array from the stack</span>
        <span class="nx">stack</span><span class="p">.</span><span class="nx">pop</span><span class="p">();</span>

        <span class="k">if</span> <span class="p">(</span><span class="nx">space</span> <span class="o">&amp;&amp;</span> <span class="nx">a</span><span class="p">.</span><span class="nx">length</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="nx">arr</span> <span class="o">?</span>
                <span class="nx">OPEN_A</span> <span class="o">+</span> <span class="nx">CR</span> <span class="o">+</span> <span class="nx">_indent</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">COMMA_CR</span><span class="p">),</span> <span class="nx">space</span><span class="p">)</span> <span class="o">+</span> <span class="nx">CR</span> <span class="o">+</span> <span class="nx">CLOSE_A</span> <span class="o">:</span>
                <span class="nx">OPEN_O</span> <span class="o">+</span> <span class="nx">CR</span> <span class="o">+</span> <span class="nx">_indent</span><span class="p">(</span><span class="nx">a</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">COMMA_CR</span><span class="p">),</span> <span class="nx">space</span><span class="p">)</span> <span class="o">+</span> <span class="nx">CR</span> <span class="o">+</span> <span class="nx">CLOSE_O</span><span class="p">;</span>
        <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
            <span class="k">return</span> <span class="nx">arr</span> <span class="o">?</span>
                <span class="nx">OPEN_A</span> <span class="o">+</span> <span class="nx">a</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">COMMA</span><span class="p">)</span> <span class="o">+</span> <span class="nx">CLOSE_A</span> <span class="o">:</span>
                <span class="nx">OPEN_O</span> <span class="o">+</span> <span class="nx">a</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="nx">COMMA</span><span class="p">)</span> <span class="o">+</span> <span class="nx">CLOSE_O</span><span class="p">;</span>
        <span class="p">}</span>
    <span class="p">}</span>

    <span class="c1">// process the input</span>
    <span class="k">return</span> <span class="nx">_serialize</span><span class="p">({</span><span class="s1">&#39;&#39;</span><span class="o">:</span><span class="nx">o</span><span class="p">},</span><span class="s1">&#39;&#39;</span><span class="p">);</span>
<span class="p">}</span>


<span class="cm">/* Public API */</span>
<span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">JSON</span> <span class="o">=</span> <span class="p">{</span>
    <span class="cm">/**</span>
<span class="cm">     * Leverage native JSON parse if the browser has a native implementation.</span>
<span class="cm">     * In general, this is a good idea.  See the Known Issues section in the</span>
<span class="cm">     * JSON user guide for caveats.  The default value is true for browsers with</span>
<span class="cm">     * native JSON support.</span>
<span class="cm">     *</span>
<span class="cm">     * @property useNativeParse</span>
<span class="cm">     * @type Boolean</span>
<span class="cm">     * @default true</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">useNativeParse</span> <span class="o">:</span> <span class="o">!!</span><span class="nx">Native</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Leverage native JSON stringify if the browser has a native</span>
<span class="cm">     * implementation.  In general, this is a good idea.  See the Known Issues</span>
<span class="cm">     * section in the JSON user guide for caveats.  The default value is true</span>
<span class="cm">     * for browsers with native JSON support.</span>
<span class="cm">     *</span>
<span class="cm">     * @property useNativeStringify</span>
<span class="cm">     * @type Boolean</span>
<span class="cm">     * @default true</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">useNativeStringify</span> <span class="o">:</span> <span class="o">!!</span><span class="nx">Native</span><span class="p">,</span>

    <span class="cm">/**</span>
<span class="cm">     * Four step determination whether a string is safe to eval. In three steps,</span>
<span class="cm">     * escape sequences, safe values, and properly placed open square brackets</span>
<span class="cm">     * are replaced with placeholders or removed.  Then in the final step, the</span>
<span class="cm">     * result of all these replacements is checked for invalid characters.</span>
<span class="cm">     *</span>
<span class="cm">     * @method isSafe</span>
<span class="cm">     * @param str {String} JSON string to be tested</span>
<span class="cm">     * @return {boolean} is the string safe for eval?</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">isSafe</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">s</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">_isSafe</span><span class="p">(</span><span class="nx">_prepare</span><span class="p">(</span><span class="nx">s</span><span class="p">));</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * &lt;p&gt;Parse a JSON string, returning the native JavaScript</span>
<span class="cm">     * representation.&lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;p&gt;When lang.JSON.useNativeParse is true, this will defer to the native</span>
<span class="cm">     * JSON.parse if the browser has a native implementation.  Otherwise, a</span>
<span class="cm">     * JavaScript implementation based on http://www.json.org/json2.js</span>
<span class="cm">     * is used.&lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * @method parse</span>
<span class="cm">     * @param s {string} JSON string data</span>
<span class="cm">     * @param reviver {function} (optional) function(k,v) passed each key:value</span>
<span class="cm">     *          pair of object literals, allowing pruning or altering values</span>
<span class="cm">     * @return {MIXED} the native JavaScript representation of the JSON string</span>
<span class="cm">     * @throws SyntaxError</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">parse</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="nx">reviver</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">Native</span> <span class="o">&amp;&amp;</span> <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">useNativeParse</span> <span class="o">?</span>
            <span class="nx">Native</span><span class="p">.</span><span class="nx">parse</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="nx">reviver</span><span class="p">)</span> <span class="o">:</span> <span class="nx">_parse</span><span class="p">(</span><span class="nx">s</span><span class="p">,</span><span class="nx">reviver</span><span class="p">);</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * &lt;p&gt;Converts an arbitrary value to a JSON string representation.&lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;p&gt;Objects with cyclical references will trigger an exception.&lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;p&gt;If a whitelist is provided, only matching object keys will be</span>
<span class="cm">     * included.  Alternately, a replacer function may be passed as the</span>
<span class="cm">     * second parameter.  This function is executed on every value in the</span>
<span class="cm">     * input, and its return value will be used in place of the original value.</span>
<span class="cm">     * This is useful to serialize specialized objects or class instances.&lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;p&gt;If a positive integer or non-empty string is passed as the third</span>
<span class="cm">     * parameter, the output will be formatted with carriage returns and</span>
<span class="cm">     * indentation for readability.  If a String is passed (such as &quot;\t&quot;) it</span>
<span class="cm">     * will be used once for each indentation level.  If a number is passed,</span>
<span class="cm">     * that number of spaces will be used.&lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * &lt;p&gt;When lang.JSON.useNativeStringify is true, this will defer to the</span>
<span class="cm">     * native JSON.stringify if the browser has a native implementation.</span>
<span class="cm">     * Otherwise, a JavaScript implementation is used.&lt;/p&gt;</span>
<span class="cm">     *</span>
<span class="cm">     * @method stringify</span>
<span class="cm">     * @param o {MIXED} any arbitrary object to convert to JSON string</span>
<span class="cm">     * @param w {Array|Function} (optional) whitelist of acceptable object keys</span>
<span class="cm">     *                  to include OR a function(value,key) to alter values</span>
<span class="cm">     *                  before serialization</span>
<span class="cm">     * @param space {Number|String} (optional) indentation character(s) or</span>
<span class="cm">     *                  depthy of spaces to format the output </span>
<span class="cm">     * @return {string} JSON string representation of the input</span>
<span class="cm">     * @throws Error</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">stringify</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="nx">w</span><span class="p">,</span><span class="nx">space</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">return</span> <span class="nx">Native</span> <span class="o">&amp;&amp;</span> <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">useNativeStringify</span> <span class="o">?</span>
            <span class="nx">Native</span><span class="p">.</span><span class="nx">stringify</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="nx">w</span><span class="p">,</span><span class="nx">space</span><span class="p">)</span> <span class="o">:</span> <span class="nx">_stringify</span><span class="p">(</span><span class="nx">o</span><span class="p">,</span><span class="nx">w</span><span class="p">,</span><span class="nx">space</span><span class="p">);</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * Serializes a Date instance as a UTC date string.  Used internally by</span>
<span class="cm">     * the JavaScript implementation of stringify.  If you need a different</span>
<span class="cm">     * Date serialization format, override this method.  If you change this,</span>
<span class="cm">     * you should also set useNativeStringify to false, since native JSON</span>
<span class="cm">     * implementations serialize Dates per the ECMAScript 5 spec.  You&#39;ve been</span>
<span class="cm">     * warned.</span>
<span class="cm">     *</span>
<span class="cm">     * @method dateToString</span>
<span class="cm">     * @param d {Date} The Date to serialize</span>
<span class="cm">     * @return {String} stringified Date in UTC format YYYY-MM-DDTHH:mm:SSZ</span>
<span class="cm">     * @static</span>
<span class="cm">     */</span>
    <span class="nx">dateToString</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">d</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">function</span> <span class="nx">_zeroPad</span><span class="p">(</span><span class="nx">v</span><span class="p">)</span> <span class="p">{</span>
            <span class="k">return</span> <span class="nx">v</span> <span class="o">&lt;</span> <span class="mi">10</span> <span class="o">?</span> <span class="s1">&#39;0&#39;</span> <span class="o">+</span> <span class="nx">v</span> <span class="o">:</span> <span class="nx">v</span><span class="p">;</span>
        <span class="p">}</span>

        <span class="k">return</span> <span class="nx">d</span><span class="p">.</span><span class="nx">getUTCFullYear</span><span class="p">()</span>         <span class="o">+</span> <span class="s1">&#39;-&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getUTCMonth</span><span class="p">()</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;-&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getUTCDate</span><span class="p">())</span>      <span class="o">+</span> <span class="s1">&#39;T&#39;</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getUTCHours</span><span class="p">())</span>     <span class="o">+</span> <span class="nx">COLON</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getUTCMinutes</span><span class="p">())</span>   <span class="o">+</span> <span class="nx">COLON</span> <span class="o">+</span>
            <span class="nx">_zeroPad</span><span class="p">(</span><span class="nx">d</span><span class="p">.</span><span class="nx">getUTCSeconds</span><span class="p">())</span>   <span class="o">+</span> <span class="s1">&#39;Z&#39;</span><span class="p">;</span>
    <span class="p">},</span>

    <span class="cm">/**</span>
<span class="cm">     * Reconstitute Date instances from the default JSON UTC serialization.</span>
<span class="cm">     * Reference this from a reviver function to rebuild Dates during the</span>
<span class="cm">     * parse operation.</span>
<span class="cm">     *</span>
<span class="cm">     * @method stringToDate</span>
<span class="cm">     * @param str {String} String serialization of a Date</span>
<span class="cm">     * @return {Date}</span>
<span class="cm">     */</span>
    <span class="nx">stringToDate</span> <span class="o">:</span> <span class="kd">function</span> <span class="p">(</span><span class="nx">str</span><span class="p">)</span> <span class="p">{</span>
        <span class="kd">var</span> <span class="nx">m</span> <span class="o">=</span> <span class="nx">str</span><span class="p">.</span><span class="nx">match</span><span class="p">(</span><span class="sr">/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.(\d{3}))?Z$/</span><span class="p">);</span>
        <span class="k">if</span> <span class="p">(</span><span class="nx">m</span><span class="p">)</span> <span class="p">{</span>
            <span class="kd">var</span> <span class="nx">d</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">();</span>
            <span class="nx">d</span><span class="p">.</span><span class="nx">setUTCFullYear</span><span class="p">(</span><span class="nx">m</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="nx">m</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="nx">m</span><span class="p">[</span><span class="mi">3</span><span class="p">]);</span>
            <span class="nx">d</span><span class="p">.</span><span class="nx">setUTCHours</span><span class="p">(</span><span class="nx">m</span><span class="p">[</span><span class="mi">4</span><span class="p">],</span> <span class="nx">m</span><span class="p">[</span><span class="mi">5</span><span class="p">],</span> <span class="nx">m</span><span class="p">[</span><span class="mi">6</span><span class="p">],</span> <span class="p">(</span><span class="nx">m</span><span class="p">[</span><span class="mi">7</span><span class="p">]</span> <span class="o">||</span> <span class="mi">0</span><span class="p">));</span>
            <span class="k">return</span> <span class="nx">d</span><span class="p">;</span>
        <span class="p">}</span>
        <span class="k">return</span> <span class="nx">str</span><span class="p">;</span>
    <span class="p">}</span>
<span class="p">};</span>

<span class="cm">/**</span>
<span class="cm"> * &lt;p&gt;Four step determination whether a string is safe to eval. In three steps,</span>
<span class="cm"> * escape sequences, safe values, and properly placed open square brackets</span>
<span class="cm"> * are replaced with placeholders or removed.  Then in the final step, the</span>
<span class="cm"> * result of all these replacements is checked for invalid characters.&lt;/p&gt;</span>
<span class="cm"> *</span>
<span class="cm"> * &lt;p&gt;This is an alias for isSafe.&lt;/p&gt;</span>
<span class="cm"> *</span>
<span class="cm"> * @method isValid</span>
<span class="cm"> * @param str {String} JSON string to be tested</span>
<span class="cm"> * @return {boolean} is the string safe for eval?</span>
<span class="cm"> * @static</span>
<span class="cm"> * @deprecated use isSafe</span>
<span class="cm"> */</span>
<span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">isValid</span> <span class="o">=</span> <span class="nx">YAHOO</span><span class="p">.</span><span class="nx">lang</span><span class="p">.</span><span class="nx">JSON</span><span class="p">.</span><span class="nx">isSafe</span><span class="p">;</span>

<span class="p">})();</span>
</pre></div>

                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div id="moduleList" class="module">
                        <h4>Modules</h4>
                        <ul class="content">
                                <li class=""><a href="module_animation.html" title="animation">animation</a></li>
                                <li class=""><a href="module_autocomplete.html" title="autocomplete">autocomplete</a></li>
                                <li class=""><a href="module_button.html" title="button">button</a></li>
                                <li class=""><a href="module_calendar.html" title="calendar">calendar</a></li>
                                <li class=""><a href="module_carousel.html" title="carousel">carousel</a></li>
                                <li class=""><a href="module_charts.html" title="charts">charts</a></li>
                                <li class=""><a href="module_colorpicker.html" title="colorpicker">colorpicker</a></li>
                                <li class=""><a href="module_connection.html" title="connection">connection</a></li>
                                <li class=""><a href="module_container.html" title="container">container</a></li>
                                <li class=""><a href="module_cookie.html" title="cookie">cookie</a></li>
                                <li class=""><a href="module_datasource.html" title="datasource">datasource</a></li>
                                <li class=""><a href="module_datatable.html" title="datatable">datatable</a></li>
                                <li class=""><a href="module_datemath.html" title="datemath">datemath</a></li>
                                <li class=""><a href="module_dom.html" title="dom">dom</a></li>
                                <li class=""><a href="module_dragdrop.html" title="dragdrop">dragdrop</a></li>
                                <li class=""><a href="module_editor.html" title="editor">editor</a></li>
                                <li class=""><a href="module_element.html" title="element">element</a></li>
                                <li class=""><a href="module_element-delegate.html" title="element-delegate">element-delegate</a></li>
                                <li class=""><a href="module_event.html" title="event">event</a></li>
                                <li class=""><a href="module_event-delegate.html" title="event-delegate">event-delegate</a></li>
                                <li class=""><a href="module_event-mouseenter.html" title="event-mouseenter">event-mouseenter</a></li>
                                <li class=""><a href="module_event-simulate.html" title="event-simulate">event-simulate</a></li>
                                <li class=""><a href="module_get.html" title="get">get</a></li>
                                <li class=""><a href="module_history.html" title="history">history</a></li>
                                <li class=""><a href="module_imagecropper.html" title="imagecropper">imagecropper</a></li>
                                <li class=""><a href="module_imageloader.html" title="imageloader">imageloader</a></li>
                                <li class="selected"><a href="module_json.html" title="json">json</a></li>
                                <li class=""><a href="module_layout.html" title="layout">layout</a></li>
                                <li class=""><a href="module_logger.html" title="logger">logger</a></li>
                                <li class=""><a href="module_menu.html" title="menu">menu</a></li>
                                <li class=""><a href="module_paginator.html" title="paginator">paginator</a></li>
                                <li class=""><a href="module_profiler.html" title="profiler">profiler</a></li>
                                <li class=""><a href="module_profilerviewer.html" title="profilerviewer">profilerviewer</a></li>
                                <li class=""><a href="module_progressbar.html" title="progressbar">progressbar</a></li>
                                <li class=""><a href="module_resize.html" title="resize">resize</a></li>
                                <li class=""><a href="module_selector.html" title="selector">selector</a></li>
                                <li class=""><a href="module_slider.html" title="slider">slider</a></li>
                                <li class=""><a href="module_storage.html" title="Storage">Storage</a></li>
                                <li class=""><a href="module_stylesheet.html" title="stylesheet">stylesheet</a></li>
                                <li class=""><a href="module_swf.html" title="swf">swf</a></li>
                                <li class=""><a href="module_swfdetect.html" title="swfdetect">swfdetect</a></li>
                                <li class=""><a href="module_swfstore.html" title="swfstore">swfstore</a></li>
                                <li class=""><a href="module_tabview.html" title="tabview">tabview</a></li>
                                <li class=""><a href="module_treeview.html" title="treeview">treeview</a></li>
                                <li class=""><a href="module_uploader.html" title="uploader">uploader</a></li>
                                <li class=""><a href="module_yahoo.html" title="yahoo">yahoo</a></li>
                                <li class=""><a href="module_yuiloader.html" title="yuiloader">yuiloader</a></li>
                                <li class=""><a href="module_yuitest.html" title="yuitest">yuitest</a></li>
                        </ul>
                    </div>

                    <div id="classList" class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="YAHOO.lang.JSON.html" title="YAHOO.lang.JSON">YAHOO.lang.JSON</a></li>
                        </ul>
                    </div>

                    <div id="fileList" class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class="selected"><a href="JSON.js.html" title="JSON.js">JSON.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2010 Yahoo! Inc. All rights reserved.
	</div>
</div>
<script type="text/javascript">

    var ALL_YUI_PROPS = [{"access": "private", "host": "YAHOO.lang.JSON", "name": "_BRACKETS", "url": "YAHOO.lang.JSON.html#property__BRACKETS", "type": "property"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_CHARS", "url": "YAHOO.lang.JSON.html#property__CHARS", "type": "property"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "dateToString", "url": "YAHOO.lang.JSON.html#method_dateToString", "type": "method"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_ESCAPES", "url": "YAHOO.lang.JSON.html#property__ESCAPES", "type": "property"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "isSafe", "url": "YAHOO.lang.JSON.html#method_isSafe", "type": "method"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "isValid", "url": "YAHOO.lang.JSON.html#method_isValid", "type": "method"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "parse", "url": "YAHOO.lang.JSON.html#method_parse", "type": "method"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_prepare", "url": "YAHOO.lang.JSON.html#method__prepare", "type": "method"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_revive", "url": "YAHOO.lang.JSON.html#method__revive", "type": "method"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_SPECIAL_CHARS", "url": "YAHOO.lang.JSON.html#property__SPECIAL_CHARS", "type": "property"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "stringify", "url": "YAHOO.lang.JSON.html#method_stringify", "type": "method"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "stringToDate", "url": "YAHOO.lang.JSON.html#method_stringToDate", "type": "method"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_UNICODE_EXCEPTIONS", "url": "YAHOO.lang.JSON.html#property__UNICODE_EXCEPTIONS", "type": "property"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_UNSAFE", "url": "YAHOO.lang.JSON.html#property__UNSAFE", "type": "property"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "useNativeParse", "url": "YAHOO.lang.JSON.html#property_useNativeParse", "type": "property"}, {"access": "", "host": "YAHOO.lang.JSON", "name": "useNativeStringify", "url": "YAHOO.lang.JSON.html#property_useNativeStringify", "type": "property"}, {"access": "private", "host": "YAHOO.lang.JSON", "name": "_VALUES", "url": "YAHOO.lang.JSON.html#property__VALUES", "type": "property"}];
</script>
</body>
</html>
